
const express = require("express");
const { getUnix } = require("../../model/tools");
const ArticleCateModel = require("../../model/articleCateModel");
const ArticleModel = require("../../model/articleModel");
const mongoose = require("../../model/core");

var router = express.Router();
router.get("/", async (req, res) => {
    var result = await ArticleCateModel.aggregate([
        {
            $lookup: {
                from: "article_cate",
                localField: "_id",
                foreignField: "pid",
                as: "items"
            }
        },
        {
            $match: {
                pid: "0"
            }
        }
    ]);
    // console.log(JSON.stringify(result,undefined,'\t'))
    res.render("admin/articleCate/index.html", {
        list: result
    })
})
router.get("/add", async (req, res) => {
    //获取顶级分类  
    var topCateList = await ArticleCateModel.find({"pid":"0"})
    res.render("admin/articleCate/add.html", {
        cateList: topCateList
    })
})

router.post("/doAdd", async (req, res) => {

    if(req.body.pid != "0"){
        req.body.pid = mongoose.Types.ObjectId(req.body.pid)
    }
    req.body.add_time = getUnix()
    var result = new ArticleCateModel(req.body);
    await result.save();
    res.render("admin/public/success.html",{
        redirectUrl:`/${req.app.locals.adminPath}/articleCate`,
        message:"添加成功"
    })
})
//回显数据
router.get("/edit", async (req, res) => {
    var id = req.query.id;
    var result = await ArticleCateModel.find({"_id":id});
    var topCateList = await ArticleCateModel.find({"pid":"0"})
    res.render("admin/articleCate/edit.html", {
        list:result[0],
        cateList: topCateList
    })
})

//修改数据
router.post("/doEdit", async (req, res) => {
    if(req.body.pid != "0"){
        req.body.pid = mongoose.Types.ObjectId(req.body.pid)
    }
    await ArticleCateModel.updateOne({"_id":req.body.id},req.body);
    res.render("admin/public/success.html",{
        redirectUrl:`/${req.app.locals.adminPath}/articleCate`,
        message:"修改成功"
    })
})


//删除数据
router.get("/delete", async (req, res) => {
    var id = req.query.id;
    var subResult = await ArticleCateModel.find({"pid":mongoose.Types.ObjectId(id)});

    if(subResult.length>0){//如果有子分类
        res.render("admin/public/error.html",{
            redirectUrl:`/${req.app.locals.adminPath}/articleCate`,
            message:"请先删除子分类"
        })
    }else{  //如果没有子分类,就是顶级分类就去判断是否有文章
        var subArticleResult = await ArticleModel.find({"cid":mongoose.Types.ObjectId(id)});
        if(subArticleResult.length>0){//如果有文章
            res.render("admin/public/error.html",{
                redirectUrl:`/${req.app.locals.adminPath}/articleCate`,
                message:"当前分类下有文章,请先删除文章"
            })
        }else{//如果没有文章
            await ArticleCateModel.deleteOne({"_id":id});
            res.render("admin/public/success.html",{
                redirectUrl:`/${req.app.locals.adminPath}/articleCate`,
                message:"删除成功"
            })
        }
     
    }
   
})
module.exports = router